// pages/classic/classic.js
import { ClassicModel } from "../../models/classic";
import { LikeModel } from "../../models/like"
const classicModel = new ClassicModel();
const likeModel = new LikeModel();
Page({

  /**
   * 页面的初始数据
   */
  data: {
    classic: {},
    isLatest: true,
    isFirst: false,
  },

  /**
   * 生命周期函数--监听页面加载
   */
  onLoad: function (options) {
    wx.showLoading({
      title:"加载中..."
    })
    classicModel.getLatest(res => {
      this.setData({
        classic: res,
        like:res.like_status,
        count:res.fav_nums
      })
    })
    wx.hideLoading()
  },
  onLike(e) {
    var behavior = e.detail.behavior;
    var id = this.data.classic.id;
    var type = this.data.classic.type;
    likeModel.getLike(behavior, id, type);
  },
  onPrev() {
    this._updateData("previous")
  },
  onNext() {
    this._updateData("next")
  },
  _updateData(NextOrPrevious) {
      classicModel.getClassic(this.data.classic.index, NextOrPrevious, res => {
        likeModel.getLikeStatus(res.type,res.id,res=>{
          this.setData({
            like:res.like_status,
            count:res.fav_nums
          })
        })
        this.setData({
          classic: res,
          isFirst: classicModel.isFirst(res.index),
          isLatest: classicModel.isLatest(res.index)
        })
      })  

  },
  

})